class Solution {
public:
    int minOperations(vector<int>& nums) {
        int n = nums.size(), gcd_all = 0, cnt1 = 0;
        for (int num : nums) {
            gcd_all = gcd(num, gcd_all);
            cnt1 += num == 1;
        }
        if (gcd_all != 1) return -1;
        if (cnt1) return n - cnt1;

        int size_min = n;
        for (int i = 0; i < n; i++) {
            int gcd_cur = 0;
            for (int j = i; j < n; j++) {
                gcd_cur = gcd(nums[j], gcd_cur);
                if (gcd_cur == 1) {
                    size_min = min(size_min, j - i);
                    break;
                }
            }
        }
        return n + size_min - 1;
    }
};